﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace KTX
{
    public partial class frmThemSinhVien : DevExpress.XtraEditors.XtraForm
    {
        Data.SinhVien _sv;
        public frmThemSinhVien(Data.SinhVien sv):this()
        {
            _sv = sv;
        }
        public frmThemSinhVien()
        {
            InitializeComponent();
            this.Shown += (a, e) =>
                {
                    if (_sv == null)
                    {
                        barThem.PerformClick();
                    }
                };
        }

        Data.QLKTXEntities db = new Data.QLKTXEntities();

        private void frmThemSinhVien_Load(object sender, EventArgs e)
        {
            lopBindingSource.DataSource = db.Lop;
            doiTuongBindingSource.DataSource = db.DoiTuong;
            if (_sv==null)
            {
                sinhVienBindingSource.DataSource = db.SinhVien.Where(p => p.MaSV == "-1");
            }
            else
            {
                sinhVienBindingSource.DataSource = _sv;
            }
        }

        private void barThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            barLuu.PerformClick();
            sinhVienBindingSource.AddNew();
        }

        private void barXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            sinhVienBindingSource.RemoveCurrent();
        }

        private void barLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            db.SaveChanges();
        }

        private void barDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void sinhVienBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Data.SinhVien()
            {
                Lop= db.Lop.First(),
                DoiTuong= db.DoiTuong.First()
            };
        }
    }
}